package com.example.bmanliku;

import android.util.Log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @description 文件工具类
 */
public class FileUtils {


    /**
     * 复制单个文件
     *
     * @param oldPath$Name String 原文件路径+文件名 如：data/user/0/com.test/files/abc.txt
     * @param newPath$Name String 复制后路径+文件名 如：data/user/0/com.test/cache/abc.txt
     * @return <code>true</code> if and only if the file was copied;
     * <code>false</code> otherwise
     */
    public static boolean copyFile(String oldPath$Name, String newPath$Name)
    {
        try
        {
            File oldFile = new File(oldPath$Name);
            if (!oldFile.exists())
            {
                Log.e("FileUtils", "copyFile:  oldFile not exist.");
                return false;
            } else if (!oldFile.isFile())
            {
                Log.e("FileUtils", "copyFile:  oldFile not file.");
                return false;
            } else if (!oldFile.canRead())
            {
                Log.e("FileUtils", "copyFile:  oldFile cannot read.");
                return false;
            }

            /* 如果不需要打log，可以使用下面的语句
            if (!oldFile.exists() || !oldFile.isFile() || !oldFile.canRead()) {
                return false;
            }
            */

            FileInputStream fileInputStream = new FileInputStream(oldPath$Name);
            FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name);
            byte[] buffer = new byte[1024];
            int byteRead;
            while (-1 != (byteRead = fileInputStream.read(buffer)))
            {
                fileOutputStream.write(buffer, 0, byteRead);
            }
            fileInputStream.close();
            fileOutputStream.flush();
            fileOutputStream.close();
            return true;
        }

        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public static String readTxtFile(String strFilePath)
    {
        //文件内容字符串
        StringBuilder content = new StringBuilder();
        //打开文件
        File file = new File(strFilePath);
        //如果path是传递过来的参数，可以做一个非目录的判断
        if (file.isDirectory())
        {
            Log.d("FileUtils", "The File doesn't not exist.");
        } else
        {
            try
            {
                InputStream instream = new FileInputStream(file);
                if (instream != null)
                {
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);
                    String line;
                    //分行读取
                    while ((line = buffreader.readLine()) != null)
                    {
                        content.append(line).append("\n");
                    }
                    instream.close();
                }
            }
            catch (java.io.FileNotFoundException e)
            {
                Log.e("FileUtils", "The File doesn't not exist.");
            }
            catch (IOException e)
            {
                Log.e("FileUtils", e.getMessage());
            }
        }
        return content.toString();
    }
}
